package pers.cj.pdos.controller;


import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.HouseWaitIn;
import pers.cj.pdos.excel.ExcelHouseIn;
import pers.cj.pdos.excel.ExcelHouseWaitIn;
import pers.cj.pdos.service.IHouseWaitInService;
import pers.cj.pdos.task.CleanTempExcelTask;
import pers.cj.pdos.utils.DateUtils;
import pers.cj.pdos.utils.FileProperUtil;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 当全部入库时，会删除该记录 前端控制器
 * </p>
 *
 * @author chenj
 * @since 2020-01-13
 */
@Controller
@RequestMapping("/house")
public class HouseWaitInController extends BaseController {
    @Autowired
    IHouseWaitInService houseWaitInService;


    @RequiresPermissions("houseIn:query")
    @RequestMapping("/houseWaitIn")
    public String houseWaitIn(){
        return "/houseIn/houseWaitIn";
    }


    @RequiresPermissions("houseIn:query")
    @RequestMapping("/houseWaitInDetail")
    public String houseWaitInDetail(Model model, String id){
        model.addAttribute("id",id);
        return "/houseIn/houseWaitInDetail";
    }

    @ResponseBody
    @RequiresPermissions("houseIn:query")
    @RequestMapping("/houseWaitIn/getList")
    public ResponseList getList(int page,
                                      int limit,
                                      String status,
                                      String type,
                                      String search,
                                      String no,
                                      String goodsName,
                                      String remark,
                                      String unitName,
                                      String makeTime1,
                                      String makeTime2,
                                      String makeUserId,
                                      String userId,
                                      String date1,
                                      String date2,
                                      String storeId,
                                      String houseId){
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<HouseWaitInVo> list = houseWaitInService.listVo(company.getId(),
                page,
                limit,
                Utils.genInteger(status),
                Utils.genInteger(type),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                Utils.genLikeData(unitName),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                makeUserId,
                PdosUtils.getViewOrderStoreId(userVo,null),
                PdosUtils.getViewOrderUserId(userVo,userId),
                Utils.genDate(date1),
                Utils.genDate(date2),
                storeId,
                houseId);

        long count = houseWaitInService.count(company.getId(),
                Utils.genInteger(status),
                Utils.genInteger(type),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                Utils.genLikeData(unitName),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                makeUserId,
                PdosUtils.getViewOrderStoreId(userVo,null),
                PdosUtils.getViewOrderUserId(userVo,userId),
                Utils.genDate(date1),
                Utils.genDate(date2),
                storeId,
                houseId);

        return ResponseList.success(count,list);
    }

    @ResponseBody
    @RequiresPermissions("houseIn:query")
    @RequestMapping("/houseWaitIn/getVoById")
    public ResponseData getVoById(String id) {
        return ResponseData.success(houseWaitInService.getVoById(id));
    }

    @ResponseBody
    @RequiresPermissions("houseIn:export")
    @RequestMapping("/houseWaitIn/export")
    public ResponseData export(String status,
                               String type,
                               String search,
                               String no,
                               String goodsName,
                               String remark,
                               String unitName,
                               String makeTime1,
                               String makeTime2,
                               String makeUserId,
                               String userId,
                               String date1,
                               String date2,
                               String storeId,
                               String houseId){
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<HouseWaitInVo> list = houseWaitInService.listVo(company.getId(),
                null,
                null,
                Utils.genInteger(status),
                Utils.genInteger(type),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                Utils.genLikeData(unitName),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                makeUserId,
                PdosUtils.getViewOrderStoreId(userVo,null),
                PdosUtils.getViewOrderUserId(userVo,userId),
                Utils.genDate(date1),
                Utils.genDate(date2),
                storeId,
                houseId);

        String fileName = "待入库单列表" + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path+fileName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX,true);
            Sheet sheet1 = new Sheet(1,0, ExcelHouseWaitIn.class);
            sheet1.setSheetName("待入库单信息");
            writer.write(genExcelHouseInList(list), sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(fileName,new Date().getTime());
            return ResponseData.success(fileName);
        } catch (IOException e) {
            return ResponseData.fail("订单列表导出失败");
        }
    }

    private List<ExcelHouseWaitIn> genExcelHouseInList(List<HouseWaitInVo> houseWaitInVoList) {
        List<ExcelHouseWaitIn> list = new ArrayList<>();
        for (HouseWaitInVo vo : houseWaitInVoList){
            ExcelHouseWaitIn excel = new ExcelHouseWaitIn();
            if (vo.getType() == 1){
                excel.setDate(DateUtils.getDate(vo.getInGoodsVo().getDate(), DateUtils.YYYY_MM_DD));
                excel.setNo(vo.getInGoodsVo().getNo());
                excel.setUnit(vo.getInGoodsVo().getProvider().getName());
                excel.setTypeName("进货");
            } else if (vo.getType() == 2){
                excel.setDate(DateUtils.getDate(vo.getRefundVo().getDate(), DateUtils.YYYY_MM_DD));
                excel.setNo(vo.getRefundVo().getNo());
                excel.setUnit(vo.getRefundVo().getCustom().getName());
                excel.setTypeName("销售退货");
            } else if (vo.getType() == 3){
                excel.setDate(DateUtils.getDate(vo.getGoodsTransferVo().getDate(), DateUtils.YYYY_MM_DD));
                excel.setNo(vo.getGoodsTransferVo().getNo());
                excel.setUnit("");
                excel.setTypeName("调拨");
            } else if (vo.getType() == 4){
                excel.setDate(DateUtils.getDate(vo.getBorrowInVo().getDate(), DateUtils.YYYY_MM_DD));
                excel.setNo(vo.getBorrowInVo().getNo());
                excel.setUnit(vo.getBorrowInVo().getProvider().getName());
                excel.setTypeName("借入");
            } else if (vo.getType() == 5){
                excel.setDate(DateUtils.getDate(vo.getBorrowOutBackVo().getDate(), DateUtils.YYYY_MM_DD));
                excel.setNo(vo.getBorrowOutBackVo().getNo());
                excel.setUnit(vo.getBorrowOutBackVo().getCustom().getName());
                excel.setTypeName("借出归还");
            }

            excel.setStore(vo.getHouseVo().getStore().getName());
            excel.setHouse(vo.getHouseVo().getName());

            String goods = "";
            BigDecimal waitInNumber = new BigDecimal("0");
            for (HouseWaitInGoodsVo hwgv : vo.getHouseWaitInGoodsVoList()){
                waitInNumber = waitInNumber.add(hwgv.getWaitInNumber());
                goods += hwgv.getGoodsVo().getName() + "、";
            }


            for (HouseWaitInPacketVo hwgv : vo.getHouseWaitInPacketVoList()){
                waitInNumber = waitInNumber.add(hwgv.getWaitInNumber());
                goods += hwgv.getPacketName() + "、";
            }

            if (!Utils.isEmpty(goods)) {
                goods = goods.substring(0,goods.length()-1);
            }

            excel.setGoods(goods);
            excel.setWaitInNumber(PdosUtils.getNumber(waitInNumber, getBusinessSet()));
            if (vo.getStatus() == 1){
                excel.setStatus("未入库");
            } else if (vo.getStatus() == 2){
                excel.setStatus("部分入库");
            } else if (vo.getStatus() == 3){
                excel.setStatus("全部入库");
            }
            list.add(excel);
        }
        return list;
    }

}